package portBroadcasting;

import WhiskeyGui.Temperatures;
//import org.apache.xmlbeans.impl.xb.xsdschema.Public;

import java.lang.*;
import java.io.*;
import java.net.*;
import java.net.InetAddress;


/**
 * User: Mike
 * Date: 12/12/11
 */

// This class creates a client that listens to the the server and prints out the temperature that it receives from the server.
public class TempClient implements Temperatures {

    private String ColumnTempC = "98.6";
    private String ColumnTempF = "98.6";
    private String AmbientTempC = "98.6";
    private String AmbientTempF = "98.6";
    private Long time;
    private MulticastSocket socket;
    private DatagramPacket packet;
    private InetAddress address;

    public static void main(String args[]) throws Exception {
//        TempClient tClient = new TempClient();
//        tClient.Multicast();
    }

    public TempClient() {

        try {
            address = InetAddress.getByName(Server.I_NET_ADD);
            socket = new MulticastSocket(Server.MULTI_PORT);
            socket.joinGroup(address);                                      //join a Multicast group and send the group salutations
            byte[] data = new byte[256];
            packet = new DatagramPacket(data, data.length);
            socket.receive(packet);
            String str = new String(packet.getData());
            System.out.println(" Temperature signal received from " + packet.getAddress() + " [time,ColTempC, ColTempF, AmbientTempC, AmbientTempF]: " + str);
            ParseMessage(str);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void updateTemps() {
        try {
            byte[] data = new byte[256];
            packet = new DatagramPacket(data, data.length);
            socket.receive(packet);
            String str = new String(packet.getData());
//            System.out.println(" Temperature signal received from " + packet.getAddress() + " [time,ColTempC, ColTempF, AmbientTempC, AmbientTempF]: " + str);
            ParseMessage(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getColumnTempC() {
        return this.ColumnTempC;
    }

    public String getColumnTempF() {
        return this.ColumnTempF;
    }

    public String getAmbientTempC() {
        return this.AmbientTempC;
    }

    public String getAmbientTempF() {
        return this.AmbientTempF;
    }

    public Long getTime() {
        return this.time;
    }

    private void Multicast() throws Exception {
        MulticastSocket socket;
        DatagramPacket packet;
        InetAddress address;
        address = InetAddress.getByName(Server.I_NET_ADD);
        socket = new MulticastSocket(Server.MULTI_PORT);
        socket.joinGroup(address);                                      //join a Multicast group and send the group salutations
        byte[] data = new byte[256];
        packet = new DatagramPacket(data, data.length);
        boolean receiveMessages = true;
        while (receiveMessages) {
            socket.receive(packet);
            String str = new String(packet.getData());
            System.out.println(" Temperature signal received from " + packet.getAddress() + " [time,ColTempC, ColTempF, AmbientTempC, AmbientTempF]: " + str);
            ParseMessage(str);
        }
    }

    private void ParseMessage(String message) {
        String[] splitarray;
        splitarray = message.split(",");
        if (splitarray.length > 4) {
            this.time = Long.valueOf(splitarray[0]);
            this.ColumnTempC = splitarray[1];
            this.ColumnTempF = splitarray[2];
            this.AmbientTempC = splitarray[3];
            this.AmbientTempF = splitarray[4];
        }
    }

    //..................................................................................................................
    // this is for a different protocol - one computer at a time communication rather than broadcasting.................
    //..................................................................................................................
    private static void TCPconn() throws IOException {
        Socket sock = null;
//        DataInputStream dis = null;                                                   // can't see why i need this...
        PrintStream ps;
        System.out.println(" Trying to connect");
        try {
            InetAddress ip = InetAddress.getByName("Scarpa");                           // to get the ip address of the server by the name
            sock = new Socket(ip, Server.TCP_PORT);                                     // Creates a socket with the server bind to it.
            ps = new PrintStream(sock.getOutputStream());
            ps.println(" Hi from client");
            DataInputStream is = new DataInputStream(sock.getInputStream());
            System.out.println(is.readLine());
        } catch (SocketException e) {
            System.out.println("SocketException " + e);
        } catch (IOException e) {
            System.out.println("IOException " + e);
        } finally {
            try {
                assert sock != null;
                sock.close();                                                           // Finally closing the socket from the client side
            } catch (IOException ie) {
                System.out.println(" Close Error   :" + ie.getMessage());
            }
        }
    }
}